package _07randomAccessFile;

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo {
    public static void main(String[] args) throws IOException {
        // RandomAcessFile
        // 访问文件任意位置
        testWrite();

//		testRead();
    }

    private static void testRead() throws IOException {
        RandomAccessFile ra = new RandomAccessFile("d:\\stuInfo.txt", "rw");

        for (int i = 0; i < 100; i++) {
            System.out.println(ra.readInt());
        }

//		void seek(long pos) 
//      设置到此文件开头测量到的文件指针偏移量，在该位置发生下一个读取或写入操作。

        // 设定到指定位置
        ra.seek(4 * 50);

        System.out.println(ra.readInt());

        for (int i = 0; i < 50; i++) {
            ra.writeInt(49);
        }
        // 3
        ra.close();
    }

    private static void testWrite() throws IOException {
        // TODO Auto-generated method stub

        // 1.
        RandomAccessFile f = new RandomAccessFile("d:\\stuInfo.txt", "rw");

        // 2.
        for (int i = 0; i < 100; i++) {
            f.writeInt(i);
        }

        // 3
        f.close();


    }
}